Ti.include('map_view.js', 'taille_edit_win.js');

SplitView = {
	windows:[],
	customer:null,
	order:null,
	item:null,
	size:null
};

function createWindow(params) {
	var win = Ti.UI.createWindow(params);
	SplitView.windows.push(win);
	
	var actionsDialog = Ti.UI.createOptionDialog({
		title:'Votre choix',
		options:['Se déconnecter', 'Annuler']
	});
	actionsDialog.addEventListener('click', function(e) {
		if (e.index == 0) {
			LoginView.win.open();
			
			// on ferme toutes les fenetres
			for (var i in SplitView.windows) {
				if (i >= 0) {
					var win = SplitView.windows[i];
					if (!win.root) {
						SplitView.detailNav.close(win, {animated:false});
					}
				}
			}
			
			SplitView.articleView.borderView.hide();
			SplitView.commandeView.borderView.hide();
			
			SplitView.splitView.close();
		}
	});
	
	var btAction = Ti.UI.createButton({
		systemButton:Ti.UI.iPhone.SystemButton.ACTION
	});
	btAction.addEventListener('click', function(e) {
		// on passe la view pour centrer la dialog dessus
		actionsDialog.show({view:btAction, animated:true});
	});
	win.rightNavButton = btAction;
	
	return win;
}

function createEnclosedView(view, params) {
	var enclosedView  = Ti.UI.createView(params);
	enclosedView.height = 'auto';
	enclosedView.add(view);
	return enclosedView;
}

function createHeader(section) {
	var header = Ti.UI.createView({
		top:styles.top,
		left:styles.left,
		bottom:styles.bottom,
		right:styles.right,
		height:'auto'
	});
	var enclosedView = createEnclosedView(header, {
		backgroundColor:styles.headerBackgroundColor
	});
	section.headerView = enclosedView;
	
	return header;
}

function createRow(row) {
	var view = Ti.UI.createView({
		top:styles.top,
		left:styles.left,
		bottom:styles.bottom,
		right:styles.right,
		height:'auto'
	});
	row.add(view);
	
	return view;
}

function createMasterView(visible) {
	var view = Ti.UI.createView({
		top:styles.top,
		left:styles.left,
		bottom:styles.bottom,
		right:styles.right,
		height:'auto'
	});
	
	var borderView = createEnclosedView(view, {
		top:styles.top,
		left:styles.left,
		right:styles.right,
		height:'auto',
		borderWidth:1,
		borderRadius:10,
		borderColor:styles.backgroundColor,
		// pour l'animation de fermeture
		backgroundColor:'AF5011',
		backgroundGradient:{
			type:'linear',
			colors:[{color:'AF5011', position:0.0}, {color:'AF5011', position:0.7}, {color:'833C0D', position:1.0}]
		},
		opacity:0.9,
		visible:visible
	});
	SplitView.masterWindow.add(borderView);
	
	view.borderView = borderView;
	
	return view;
}

function createMasterLbl(params) {
	var lbl = Ti.UI.createLabel({
		top:0,
		left:0,
		width:'auto',
		height:'auto',
		color:styles.masterViewColor,
		shadowColor:'585858',
		shadowOffset:{x:-1, y:-1}
	});
	
	// on set les paramètres passés en paramètre
	for (var param in params) {
		// pour supprimer le warning
		if (param) {
			lbl[param] = params[param];
		}
	}
	
	return lbl;
}

function calculateTopView(view, aboveView) {
	view.borderView.top = bottom(aboveView.borderView) + styles.masterVGap;
}

function animateShowMasterView(view) {
	var borderView = view.borderView;
	borderView.isShowing = true;
	borderView.left = 300;
	borderView.opacity = 0;
	borderView.show();
	borderView.animate({opacity:1, left:styles.left, duration:500}, function() {
		borderView.isShowing = false;
	});
}

function animateHideMasterView(view) {
	var borderView = view.borderView;
	if (borderView.visible) {
		borderView.animate({opacity:0, left:300, duration:500}, function() {
			if (!borderView.isShowing) {
				borderView.hide();
			}
		});
	}
}

// WINDOWS
SplitView.masterWindow = Ti.UI.createWindow({
	title:'Récapitulatif',
	//backgroundColor:styles.masterViewBackgroundColor
	backgroundGradient:{
		type:'linear',
		colors:[styles.masterViewBackgroundColor, 'white'],
		startPoint:{x:0, y:0},
		endPoint:{x:0, y:2000}
	}
});

var mapWin = createWindow({
	title:'Mes clients',
	root:true
});

// MASTER NAV GROUP
var masterNav = Ti.UI.iPhone.createNavigationGroup({
	window:SplitView.masterWindow
});


// DETAIL NAV GROUP
SplitView.detailNav = Ti.UI.iPhone.createNavigationGroup({
	window:mapWin
});


// SPLIT VIEW
SplitView.splitView = Ti.UI.iPad.createSplitWindow({
	masterView:masterNav,
	detailView:SplitView.detailNav
});


// MASTER
var userView = createMasterView(true);
var lblUser;
function initUserView() {
	lblUser = createMasterLbl();
	userView.add(lblUser);
}
initUserView();

var clientView = createMasterView(false);
var imageClient, lblClientRaisoc, lblClientAdresse;
function initClientView() {
	imageClient = Ti.UI.createImageView({
		top:0,
		left:0,
		height:40
	});
	clientView.add(imageClient);
	
	lblClientRaisoc = createMasterLbl({
		top:bottom(imageClient) + styles.vGap
	});
	clientView.add(lblClientRaisoc);
	
	lblClientAdresse = createMasterLbl({
		top:bottom(lblClientRaisoc) + styles.vGap
	});
	clientView.add(lblClientAdresse);
}
initClientView();

SplitView.commandeView = createMasterView(false);
var lblCommandeNum, lblCommandeType, lblCommandeMontant, lblCommandeDateLivraison;
function initCommandeView() {
	lblCommandeNum = createMasterLbl();
	SplitView.commandeView.add(lblCommandeNum);
	
	lblCommandeType = createMasterLbl({
		top:bottom(lblCommandeNum) + styles.vGap
	});
	SplitView.commandeView.add(lblCommandeType);
	
	lblCommandeMontant = createMasterLbl({
		top:bottom(lblCommandeType) + styles.vGap
	});
	SplitView.commandeView.add(lblCommandeMontant);
	
	lblCommandeDateLivraison = createMasterLbl({
		top:bottom(lblCommandeMontant) + styles.vGap
	});
	SplitView.commandeView.add(lblCommandeDateLivraison);
}
initCommandeView();

SplitView.articleView = createMasterView(false);
var lblArticleNum, lblArticleMSA, lblArticleCouleur, lblArticleQte, imageArticleDiv;
function initArticleView() {
	lblArticleNum = createMasterLbl();
	SplitView.articleView.add(lblArticleNum);
	
	lblArticleMSA = createMasterLbl({
		top:bottom(lblArticleNum) + styles.vGap
	});
	SplitView.articleView.add(lblArticleMSA);
	
	lblArticleCouleur = createMasterLbl({
		top:bottom(lblArticleMSA) + styles.vGap
	});
	SplitView.articleView.add(lblArticleCouleur);
	
	lblArticleQte = createMasterLbl({
		top:bottom(lblArticleCouleur) + styles.vGap
	});
	SplitView.articleView.add(lblArticleQte);
	
	var articleDivView = Ti.UI.createView({
		top:bottom(lblArticleQte) + styles.vGap,
		left:0,
		width:50,
		height:50,
		borderWidth:1,
		borderRadius:10,
		opacity:0.75,
		backgroundColor:'white'
	});
	SplitView.articleView.add(articleDivView);
	
	imageArticleDiv = Ti.UI.createImageView({
		top:articleDivView.top,
		left:articleDivView.left,
		width:articleDivView.width,
		height:articleDivView.height
	});
	SplitView.articleView.add(imageArticleDiv);
}
initArticleView();


SplitView.finalizeOrderToolbar = function() {
	var flexSpace = Titanium.UI.createButton({
		systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
	});
	
	var btSaveOrder = Ti.UI.createButton({
		style:Ti.UI.iPhone.SystemButtonStyle.DONE,
		title:'Finaliser commande'
	});
	btSaveOrder.addEventListener('click', function(e) {
		OrderWin.open(SplitView.order.number, toolbar);
	});
	
	var toolbar = Titanium.UI.createToolbar({
		items:[btSaveOrder, flexSpace],
		bottom:0,
		borderTop:true,
		borderBottom:false,
		translucent:true
	});
	
	return toolbar;
};
SplitView.tb = SplitView.finalizeOrderToolbar();


// TAILLE
function getTailleData(article) {
	var tailles = [];
	
	tailles[0] = {
		article:article,
		taille:200,
		qte:5,
		qteAnnulee:0,
		prix:50,
		remise:10,
		prixNet:45
	};
	
	return tailles;
}

function getTailleRow(rowData) {
	var row = Ti.UI.createTableViewRow({
		height:'auto',
		hasDetail:true,
		data:rowData,
		// pour l'optimisation de la récupération de ressources
		className:'taille_row'
	});
	
	var rowView = createRow(row);
	
	var imageDiv = Ti.UI.createImageView({
		image:urlImageDiv(rowData.division),
		top:0,
		left:0,
		width:100,
		height:100
	});
	rowView.add(imageDiv);
	
	var lblTaille = Ti.UI.createLabel({
		text:rowData.size,
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:right(imageDiv) + styles.hGap,
		width:'auto'
	});
	rowView.add(lblTaille);
	
	var lblQteCde = Ti.UI.createLabel({
		text:rowData.orderedQty,
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:right(lblTaille) + styles.tailleHGap,
		width:'auto'
	});
	lblQteCde.text += rowData.orderedQty > 1 ? ' articles\ncommandés' : ' article\ncommandé';
	rowView.add(lblQteCde);
	
	var lblQteAnnulee = Ti.UI.createLabel({
		text:rowData.canceledQty,
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:right(lblQteCde) + styles.tailleHGap + 30,
		width:'auto'
	});
	lblQteAnnulee.text += rowData.canceledQty > 1 ? ' articles\nannulés' : ' article\nannulé';
	rowView.add(lblQteAnnulee);
	
	return row;
}

function getTailleTableViewData(tailles) {
	var data = [], taille;
	for (var i in tailles) {
		// pour supprimer le warning
		if (i >= 0) {
			taille = tailles[i];
			if (taille.orderedQty - taille.canceledQty > 0) {
				data.push(getTailleRow(taille));
			}
		}
	}
	
	return data;
}

SplitView.tailleWin = createWindow({
	title:'Tailles'
});
SplitView.tailleWin.add(SplitView.tb);

var tailleTableView = Ti.UI.createTableView({
	bottom:SplitView.tb.height
});
tailleTableView.addEventListener('click', function(e) {
	var data = e.rowData.data;
	tailleTableView.editingRowData = data;
	TailleEditWin.open(data);
	SplitView.size = data;
	SplitView.size.selectedRow = e.index;
});
SplitView.tailleWin.add(tailleTableView);

SplitView.tailleWin.addEventListener('focus', function(e) {
	TailleEditWin.edited = TailleEditWin.edited && !equals(tailleTableView.editingRowData, TailleEditWin.data);
	if (TailleEditWin.edited) {
		showActInd();
		
		Services.updateTailleData(TailleEditWin.data, function() {
			hideActInd();
			
			tailleTableView.updateRow(SplitView.size.selectedRow,
				getTailleRow(TailleEditWin.data),
				{animationStyle:Ti.UI.iPhone.RowAnimationStyle.RIGHT});
		});
		
		TailleEditWin.edited = false;
		SplitView.articleWin.edited = true;
	}
});

function initTailleWin(article) {
	showActInd();
	
	Services.getTailleData(article, function(tailles) {
		tailleTableView.data = getTailleTableViewData(tailles);
	
		SplitView.detailNav.open(SplitView.tailleWin);
		hideActInd();
	});
}


// ARTICLE
function getArticleData() {
	var articles = [];
	
	articles[0] = {
		num:'001',
		marque:'VO',
		saison:'H',
		annee:'2011',
		designation:'Désignation de l article',
		couleur:'rouge',
		qte:'10',
		div:'ski',
		hasChild:true
	};
	
	articles[1] = {
		num:'001',
		marque:'VO',
		saison:'H',
		annee:'2011',
		designation:'Désignation de l article',
		couleur:'jaune',
		qte:'10',
		div:'ski',
		hasChild:false
	};
	
	articles[2] = {
		num:'002',
		marque:'VO',
		saison:'H',
		annee:'2011',
		designation:'G-FORCE POWER FULL SUSP.IQ 2ND;------------------------------',
		couleur:'jaune',
		qte:'10',
		div:'ski',
		hasChild:true
	};
	
	return articles;
}

function getArticleRow(rowData) {
	var row = Ti.UI.createTableViewRow({
		height:'auto',
		data:rowData,
		// pour la recherche sur la référence
		filter:rowData.code,
		hasChild:true,
		// pour l'optimisation de la récupération de ressources
		className:'article_row'
	});
	
	var rowView = createRow(row);
	rowView.left = 50;
	
	var lblCouleur = Ti.UI.createLabel({
		text:rowData.color,
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:0,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblCouleur);
	
	var lblQte = Ti.UI.createLabel({
		text:'Qté :\n' + rowData.quantity,
		font:styles.rowFont,
		color:styles.rowColor,
		//top:bottom(lblCouleur) + styles.vGap,
		bottom:0,
		left:0,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblQte);
	
	var imageDiv = Ti.UI.createImageView({
		image:urlImageDiv(rowData.division),
		top:0,
		left:200,
		//width:'auto',
		height:100
	});
	rowView.add(imageDiv);
	
	return row;
}

function getArticleTableViewData(articles) {
	var data = [], article, previousNum = -1, num;
	var section, header, lblNum, lblMSA, lblDesignation, imageBrand, imageSeason;
	for (var i in articles) {
		// pour supprimer le warning
		if (i >= 0) {
			article = articles[i];
			num = article.code;
			
			if (previousNum !== num) {
				section = Ti.UI.createTableViewSection();
				header = createHeader(section);
				
				lblNum = Ti.UI.createLabel({
					text:num,
					font:styles.headerFont,
					color:styles.headerColor,
					top:0,
					left:0,
					width:'auto',
					height:'auto'
				});
				header.add(lblNum);
				
				lblMSA = Ti.UI.createLabel({
					text:formatMSA(article),
					font:styles.headerFont,
					color:styles.headerColor,
					//top:0,
					bottom:0,
					left:0,
					//left:styles.articleHGap,
					width:'auto',
					height:'auto'
				});
				header.add(lblMSA);
				
				imageSeason = Ti.UI.createImageView({
					image:urlImageSeason(article.season),
					top:0,
					//left:right(imageBrand) + 80,
					right:0,
					width:50
					//height:50
				});
				header.add(imageSeason);
				
				imageBrand = Ti.UI.createImageView({
					image:urlImageBrand(article.brandCode),
					top:0,
					//left:right(lblMSA) + styles.articleHGap,
					right:imageSeason.width + 50,
					width:150,
					height:80,
					borderWidth:1,
					borderRadius:10
				});
				header.add(imageBrand);
				
				lblDesignation = Ti.UI.createLabel({
					text:article.designation.substring(0, article.designation.lastIndexOf(';') - 1),
					font:styles.headerFont,
					color:styles.headerColor,
					//top:bottom(lblNum) + styles.vGap,
					top:0,
					left:right(lblMSA) + styles.articleHGap,
					//left:0,
					width:'auto'
					//height:'auto'
				});
				header.add(lblDesignation);
				
				data.push(section);
			}
			
			section.add(getArticleRow(article));
			previousNum = num;
		}
	}
	
	return data;
}

SplitView.articleWin = createWindow({
	title:'Articles'
});
SplitView.articleWin.addEventListener('focus', function(e) {
	animateHideMasterView(SplitView.articleView);
	
	if (SplitView.articleWin.edited) {
		showActInd();
		Services.getArticleData(SplitView.order, function(articles) {
			articleTableView.data = getArticleTableViewData(articles);
			hideActInd();
		});
		
		SplitView.commandeWin.edited = true;
		SplitView.articleWin.edited = false;
	}
});

var search = Ti.UI.createSearchBar({
	hintText:'Référence de l\'article',
	keyboardType:Ti.UI.KEYBOARD_NUMBER_PAD
});

var articleTableView = Ti.UI.createTableView({
	search:search,
	filterAttribute:'filter'
});
articleTableView.addEventListener('click', function(e) {
	if (e.row.hasChild) {
		var data = e.rowData.data;
		lblArticleNum.text = 'Numéro : ' + data.code;
		lblArticleMSA.text = formatMSA(data);
		lblArticleCouleur.text = data.color;
		lblArticleQte.text = 'Quantité : ' + data.quantity;
		imageArticleDiv.image = urlImageDiv(data.division);
		
		// on recalcule le top
		calculateTopView(SplitView.articleView, SplitView.commandeView);
		animateShowMasterView(SplitView.articleView);
		
		initTailleWin(data);
		SplitView.item = data;
		SplitView.item.selectedRow = e.index;
	}
});
SplitView.articleWin.add(articleTableView);

function initArticleWin(commande) {
	showActInd();
	
	Services.getArticleData(commande, function(articles) {
		articleTableView.data = getArticleTableViewData(articles);
	
		SplitView.detailNav.open(SplitView.articleWin);
		hideActInd();
	});
}


// COMMANDE
function getCommandeData(client) {
	var commandes = [];
	
	commandes[0] = {
		num:'54932147',
		type:'B',
		montant:500,
		dateCommande:new Date('05/15/2011'),
		dateLivraison:new Date('06/11/2011'),
		hasChild:false
	};
	
	commandes[1] = {
		num:'65448211',
		type:'B',
		montant:500,
		dateCommande:new Date('05/15/2011'),
		dateLivraison:new Date('06/11/2011'),
		hasChild:true
	};
	
	commandes[2] = {
		num:'65138211',
		type:'B',
		montant:200,
		dateCommande:new Date('04/15/2011'),
		dateLivraison:new Date('06/11/2011'),
		hasChild:true
	};
	
	return commandes;
}

function getCommandeRow(rowData) {
	var row = Ti.UI.createTableViewRow({
		height:'auto',
		data:rowData,
		// pour la recherche sur le numéro de commande
		filter:rowData.number,
		hasChild:true,
		// pour l'optimisation de la récupération de ressources
		className:'commande_row'
	});
	
	var rowView = createRow(row);
	
	var lblNum = Ti.UI.createLabel({
		text:'Num :\n' + rowData.number,
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:0,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblNum);
	
	var typeView = Ti.UI.createView({
		//top:bottom(lblNum) + 10,
		bottom:0,
		left:0,
		width:50,
		height:50,
		borderRadius:10,
		backgroundGradient:{
			type:'linear',
			colors:[{color:'626262', position:0.0}, {color:'222', position:1.0}]
		}
	});
	rowView.add(typeView);
	
	var lblType = Ti.UI.createLabel({
		text:rowData.kind,
		font:styles.rowFont,
		color:'white',
		textAlign:'center'
	});
	typeView.add(lblType);
	
	var lblDateCde = Ti.UI.createLabel({
		text:'Cde :\n' + formatDateDayMonthYear(sqlToJsDate(rowData.orderDate)),
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:styles.commandeDateHGap,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblDateCde);
	
	var lblDateLiv = Ti.UI.createLabel({
		text:'Liv :\n' + formatDateDayMonthYear(sqlToJsDate(rowData.deliveryDate)),
		font:styles.rowFont,
		color:styles.rowColor,
		bottom:0,
		left:lblDateCde.left,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblDateLiv);
	
	var lblQte = Ti.UI.createLabel({
		text:'Qté :\n' + rowData.qty,
		font:styles.rowFont,
		color:styles.rowColor,
		top:0,
		left:lblDateLiv.left + styles.commandeHGap,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblQte);
	
	var lblMontant = Ti.UI.createLabel({
		text:'Mnt :\n' + formatPrice(rowData.sum),
		font:styles.rowFont,
		color:styles.rowColor,
		bottom:0,
		left:lblDateLiv.left + styles.commandeHGap,
		width:'auto',
		height:'auto'
	});
	rowView.add(lblMontant);
	
	var imageSeason = Ti.UI.createImageView({
		image:urlImageSeason(rowData.season),
		top:0,
		right:0,
		width:80,
		height:50
	});
	rowView.add(imageSeason);
	
	var imageBrand = Ti.UI.createImageView({
		image:urlImageBrand(rowData.brandCode),
		top:bottom(imageSeason) + 10,
		bottom:0,
		right:0,
		width:80,
		height:typeView.height,
		borderWidth:1,
		borderRadius:10
	});
	rowView.add(imageBrand);
	
	return row;
}

function getCommandeTableViewData(commandes) {
	var data = [], previousDate, date, commande, mustSum, sum = 0;
	var section, header, lblSommeMontants, lblMoisAnnee;
	for (var i in commandes) {
		// pour supprimer le warning
		if (i >= 0) {
			commande = commandes[i];
			date = sqlToJsDate(commande.orderDate);
			
			if (i > 0) {
				mustSum = date.getMonth() !== previousDate.getMonth() || date.getFullYear() !== previousDate.getFullYear();
			}
			
			if (i == 0 || mustSum) {
				if (i > 0) {
					lblSommeMontants.text = formatPrice(sum);
					sum = 0;
				}
				
				section = Ti.UI.createTableViewSection();
				header = createHeader(section);
				
				lblMoisAnnee = Ti.UI.createLabel({
					text:formatDateMonthYear(date),
					font:styles.headerFont,
					color:styles.headerColor,
					top:0,
					left:styles.commandeDateHGap,
					width:'auto',
					height:'auto'
				});
				header.add(lblMoisAnnee);
				
				lblSommeMontants = Ti.UI.createLabel({
					font:styles.headerFont,
					color:styles.headerColor,
					top:0,
					left:lblMoisAnnee.left + styles.commandeHGap,
					width:'auto',
					height:'auto'
				});
				header.add(lblSommeMontants);
				
				data.push(section);
			}
				
			section.add(getCommandeRow(commande));
			sum += commande.sum;
			previousDate = date;
		}
	}
	
	lblSommeMontants.text = formatPrice(sum);
	
	return data;
}

SplitView.commandeWin = createWindow({
	title:'Commandes'
});
SplitView.commandeWin.addEventListener('focus', function(e) {
	animateHideMasterView(SplitView.commandeView);
	
	if (SplitView.commandeWin.edited) {
		showActInd();
		Services.getCommandeData(SplitView.customer, function(commandes) {
			commandeTableView.data = getCommandeTableViewData(commandes);
			hideActInd();
		});
		SplitView.commandeWin.edited = false;
	}
});

var search = Ti.UI.createSearchBar({
	hintText:'Numéro de commande',
	keyboardType:Ti.UI.KEYBOARD_NUMBER_PAD
});

var commandeTableView = Ti.UI.createTableView({
	search:search,
	filterAttribute:'filter'
});
commandeTableView.addEventListener('click', function(e) {
	if (e.row.hasChild) {
		var data = e.rowData.data;
		lblCommandeNum.text = 'Numéro : ' + data.number;
		lblCommandeType.text = 'Type : ' + data.kind;
		lblCommandeMontant.text = 'Montant : ' + formatPrice(data.sum);
		lblCommandeDateLivraison.text = 'Livraison : ' + formatDateDayMonthYear(sqlToJsDate(data.deliveryDate));
		
		// on recalcule le top
		calculateTopView(SplitView.commandeView, clientView);
		animateShowMasterView(SplitView.commandeView);
		
		initArticleWin(data);
		SplitView.order = data;
		SplitView.order.selectedRow = e.index;
	}
});
SplitView.commandeWin.add(commandeTableView);

function initCommandeWin(client) {
	showActInd();
	
	Services.getCommandeData(client, function(commandes) {
		commandeTableView.data = getCommandeTableViewData(commandes);
	
		SplitView.detailNav.open(SplitView.commandeWin);
		hideActInd();
	});
}


// MAP
SplitView.selectCustomer = function(client) {
	imageClient.image = urlImageCustomer(client.raisonSociale);
	lblClientRaisoc.text = client.raisonSociale;
	lblClientAdresse.text = formatAddress(client.address);
	if (lblClientRaisoc.text.length > 25) {
		lblClientAdresse.top += 15;
	} else {
		lblClientAdresse.top -= 15;
	}
	
	// on recalcule le top
	calculateTopView(clientView, userView);
	animateShowMasterView(clientView);
	
	initCommandeWin(client);
	SplitView.customer = client;
};

mapWin.addEventListener('focus', function(e) {
	animateHideMasterView(clientView);
});

function initMapWin(user) {
	showActInd();
	
	Services.getClientData(user, function(clients) {
		mapWin.add(MapView.get(clients));
		
		SplitView.splitView.open({transition:Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT});
		hideActInd();
		LoginView.win.close();
	});
}


/*----------------------------------------------------------*/
SplitView.open = function(user) {
	lblUser.text = user.raisonSociale;
	
	initMapWin(user);
};